import os
import cgi
import wsgiref.handlers
import logging
import urlparse
import re

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.api import urlfetch
from google.appengine.api import users
from google.appengine.ext.webapp.util import login_required
from google.appengine.api import memcache

class Site(db.Model):
  url = db.LinkProperty(required=True)
  linked = db.BooleanProperty(default=False)
  linkInfo = db.StringProperty()
  statusCode = db.IntegerProperty()
  size = db.IntegerProperty()
  content = db.TextProperty()
  matchCode = db.ListProperty(bool)
  
class MatchConf(db.Model):
  name = db.StringProperty(required=True)
  matchCode = db.StringProperty(required=True)
  isMatch = db.BooleanProperty(default=True)


class Index(webapp.RequestHandler):
  '''@login_required
  '''
  def get(self):
    
    matchs = memcache.get('matchs')
    if matchs is None:
      logging.info('read conf from db...')
      q = MatchConf.all()
      q.filter("isMatch = ", True)
      matchs = q.fetch(limit=20)
      memcache.add('matchs', matchs, 3600)
    
    user = users.get_current_user()
    
    path = os.path.join(os.path.dirname(__file__), 'templates/index.html')
    self.response.out.write(template.render(path, {'matchs': matchs, 'user': user}))

class CheckUrls(webapp.RequestHandler):
    
  def ParseUrl(self, url, matchs):

    if url.find('://') == -1:
      url = 'http://' + url

    site = Site(url = url)

    u = urlparse.urlparse(url)
    if u.scheme != 'http' or u.netloc == '':
      site.linkInfo = "please check link format,only support HTTP and HTTPS"
      return site

    logging.debug("Fetching Remote URL " + url + " start...")
    try:
      httpresponse = urlfetch.fetch(url, allow_truncated=True)
      if httpresponse.content_was_truncated:
        site.linkInfo = "read content too large..."
 
      site.linked = True
      site.statusCode = httpresponse.status_code
      site.content = db.Text(unicode(httpresponse.content, "iso-8859-1"))
      site.size = len(site.content)
      
      matchCode = []
      for m in matchs:
        r = re.findall(r'\b' + m, site.content)
        if len(r) > 0:
          matchCode.append(True)
        else:
          matchCode.append(False)
      
      site.matchCode = matchCode
      logging.debug("Fetching Remote URL " + url + " ok...")

    except urlfetch.InvalidURLError:
      site.linkInfo = "not access"

    except urlfetch.DownloadError:
      site.linkInfo = "please check link format,only support HTTP and HTTPS"
    
    except Exception, e:
      logging.error(e)
      pass
    
    #site.put()
    return site

  def post(self):

    urls = cgi.escape(self.request.get('content'))
    urllist = urls.split('\r\n')
    results = []
    matchs = []
    matchConfs = self.request.get_all('matchConf')
    for conf in matchConfs:
      matchs.append(conf)

    for url in urllist:
      if not url == '':
        site = self.ParseUrl(url, matchs)
        results.append(site)

    matchNames = self.request.get('checkedMatchName').split(',')
    path = os.path.join(os.path.dirname(__file__), 'templates/result.html')
    self.response.out.write(template.render(path, {'results': results, 'matchNames': matchNames}))

class Init(webapp.RequestHandler):
  def get(self):
    match = MatchConf(name = 'E网打进', matchCode = 'http://cnimg.alisoft.com//js/probe/launch.js\?site=(\d*)', isMatch = True)
    match.put()

def main():
  application = webapp.WSGIApplication(
                                       [('/', Index),
                                        ('/check', CheckUrls),
                                        ('/init', Init)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()